/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Chassis.java
 *
 * Created on Oct 27, 2010, 7:53:26 PM
 */

package lmclogistics;


import components.Dates;
import components.Text;
import components.TextArea;
import components.Image;
import components.NButton;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import sql.Sql;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sonia Tan
 */
public class EditChassis extends javax.swing.JFrame {

    /** Creates new form Chassis */
    private Image bg;

    private int selectedID;

    ButtonGroup size = new ButtonGroup();

        final JRadioButton size20 = new JRadioButton();
        final JRadioButton size40 = new JRadioButton();
        final JRadioButton sizec = new JRadioButton();

    TextArea prefix = new TextArea();
        TextArea serialNo = new TextArea();
        TextArea plateNo = new TextArea();
        Dates registryDate = new Dates();
        Dates expiryDate = new Dates();
        TextArea orNo = new TextArea();
        TextArea crNo = new TextArea();

    ButtonGroup cebu = new ButtonGroup();

        final JRadioButton cebuY = new JRadioButton();
        final JRadioButton cebuN = new JRadioButton();

    ButtonGroup damage = new ButtonGroup();

        final JRadioButton damageY = new JRadioButton();
        final JRadioButton damageN = new JRadioButton();

    public EditChassis(int id) {
        selectedID = id;

        setLayout(null);
        setTitle("Edit Chassis");
        setAlwaysOnTop(true);
        setSize(512, 488);
        setResizable(false);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        centerScreen();
        setVisible(true);

        setBg();
        okCancelButtons();
        initText();
        initFlds();        

        BufferedImage image = null;
        try {
            image = ImageIO.read(getClass().getResource("/images/favicon.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        setIconImage(image);
    }

    public final void setBg(){
        bg = new Image("panel.png",509,460);
        bg.setLocation(0,0);

        add(bg,-1);
    }

    public int getCenterX(int x){

        int a = 919/2 - x/2;


        return a;
    }

    public int getCenterY(int x){

        int a = 577/2 - x/2;


        return a;
    }

    public void okCancelButtons()
    {
        NButton cancel = new NButton("Cancel",410,420);
        NButton ok = new NButton("Ok",320 ,420);
        add(ok,0);
        add(cancel,0);
        cancel.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {                        
                        dispose();
                    }
                });
        ok.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    acceptEdit();
                    dispose();
                    //FormWarning warn = new FormWarning("incorrect");
                    }
                });
    }

    public void initText(){
        Text code = new Text();
        Text cd = new Text();
        Text size = new Text();
        Text prefix = new Text();
        Text serialNo = new Text();
        Text plateNo = new Text();
        Text registryDate = new Text();
        Text expiryDate = new Text();
        Text orNo = new Text();
        Text crNo = new Text();
        Text cebu = new Text();
        Text damage = new Text();

        code.inText("Code:", 15, 15);
        cd.inText("ABCD",100,15);
        size.inText("Size:", 15, 40);
        prefix.inText("Prefix:", 15, 65);
        serialNo.inText("Serial No.:", 15 , 90);
        plateNo.inText("Plate No.:", 15, 115);
        registryDate.inText("Registry Date:", 15, 140);
        expiryDate.inText("Expiry Date:", 15, 165);
        orNo.inText("OR No.:", 15, 190);
        crNo.inText("CR No.:", 15, 215);
        cebu.inText("CEBU", 15, 240);
        damage.inText("DAMAGE", 15, 265);

        add(code,0);
        add(cd,0);
        add(size,0);
        add(prefix,0);
        add(serialNo,0);
        add(plateNo,0);
        add(registryDate,0);
        add(expiryDate,0);
        add(orNo,0);
        add(crNo,0);
        add(cebu,0);
        add(damage,0);

    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    /*
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        getContentPane().setLayout(null);
    }// </editor-fold>//GEN-END:initComponents
*/
    private void initFlds() {
        String query = "SELECT * FROM chassis WHERE chsPin = "+selectedID;
        String qSize;
        String qCebu;
        String qDamage;
        String qPrefix;
        String qSrlNo;
        String qPltNo;
        String qRegDate;
        String qExpDate;
        String qOrNo;
        String qCrNo;

        prefix.initText(60, 25, 100, 65);
        serialNo.initText(60, 25, 100 , 90);
        plateNo.initText(60, 25, 100, 115);
        registryDate.initDates(350, 25, 120, 140);
        expiryDate.initDates(350, 25, 120, 165);
        orNo.initText(60, 25, 100, 190);
        crNo.initText(60, 25, 100, 215);

        size20.setText("20");
        size40.setText("40");
        sizec.setText("c");

        cebuY.setText("Yes");
        cebuN.setText("No");

        damageY.setText("Yes");
        damageN.setText("No");

        size20.setLocation(100,39);
        size40.setLocation(150,39);
        sizec.setLocation(200,39);
        size20.setSize(50,25);
        size40.setSize(50,25);
        sizec.setSize(50,25);

        cebuY.setLocation(100,240);
        cebuN.setLocation(150,240);
        cebuY.setSize(50,25);
        cebuN.setSize(50,25);

        damageY.setSize(50,25);
        damageN.setSize(50,25);
        damageY.setLocation(100,265);
        damageN.setLocation(150,265);

        try{
            Connection connection = Sql.getSqlConnection();
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            if(rs.next())
            {
                 qSize = rs.getString("chsSize");
                 qCebu = rs.getString("chsCebu");
                 qDamage = rs.getString("chsDamage");
                 qPrefix = rs.getString("chsPrefix");
                 qSrlNo = rs.getString("chsSrlNo");
                 qPltNo = rs.getString("chsPltNo");
                 qRegDate = rs.getString("chsRegDate");
                 qExpDate = rs.getString("chsExpDate");
                 qOrNo = rs.getString("chsORNo");
                 qCrNo = rs.getString("chsCRNo");

                 if(qSize.equals("20"))
                     size20.setSelected(true);
                 else if(qSize.equals("40"))
                     size40.setSelected(true);
                 else if(qSize.equals("c"))
                     sizec.setSelected(true);

                 if(qCebu.equals("1"))
                     cebuY.setSelected(true);
                 else
                     cebuN.setSelected(true);

                 if(qDamage.equals("1"))
                     damageY.setSelected(true);
                 else
                     damageN.setSelected(true);

                 prefix.setText(qPrefix);
                 serialNo.setText(qSrlNo);
                 plateNo.setText(qPltNo);
                 //registryDate.setText(qSrlNo);
                 //serialNo.setText(qSrlNo);
                 orNo.setText(qOrNo);
                 crNo.setText(qCrNo);
            }

            Sql.closeSqlConnection(connection);
        } catch(Exception ex){
                Logger.getLogger(EditChassis.class.getName()).log(Level.SEVERE, null, ex);
        }

        size.add(size20);
        size.add(size40);
        size.add(sizec);        

        cebu.add(cebuY);
        cebu.add(cebuN);        

        damage.add(damageY);
        damage.add(damageN);
      
        
        
        add(size20,0);
        add(size40,0);
        add(sizec,0);
        add(prefix,0);
        add(serialNo,0);
        add(plateNo,0);
        add(registryDate,0);
        add(expiryDate,0);
        add(orNo,0);
        add(crNo,0);
        add(cebuY,0);
        add(cebuN,0);
        add(damageY,0);
        add(damageN,0);
    }

    private void acceptEdit()
    {
        String cSize = "20", cCebu = "1", cDamage = "1";

        if(size20.isSelected()==true)
            cSize = "20";
        if(size40.isSelected()==true)
            cSize = "40";
        if(sizec.isSelected()==true)
            cSize = "c";

        if(cebuY.isSelected()==true)
            cCebu = "1";
        if(cebuN.isSelected()==true)
            cCebu = "0";

        if(damageY.isSelected()==true)
            cDamage = "1";
        if(damageN.isSelected()==true)
            cDamage = "0";

        String query = "UPDATE chassis SET chsSize='"+cSize+"', chsSrlNo='"+serialNo.getText()+
                "', chsPltNo='"+plateNo.getText()+"', chsRegDate='"+registryDate.getDate()+
                "', chsExpDate='"+expiryDate.getDate()+"', chsORNo='"+orNo.getText()+"', chsCRNo='"+crNo.getText()+
                "', chsCebu='"+cCebu+"', chsDamage='"+cDamage+"' WHERE chsPin="+selectedID;

        try{
            Connection connection = Sql.getSqlConnection();
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.executeUpdate();

            Sql.closeSqlConnection(connection);
        } catch(Exception ex)
        {
            Logger.getLogger(EditChassis.class.getName()).log(Level.SEVERE, null, ex);
        }

    }


    public void centerScreen(){

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    // Determine the new location of the window
    int w = getSize().width;
    int h = getSize().height;
    int x = (dim.width-w)/2;
    int y = (dim.height-h)/2;

    // Move the window
    setLocation(x, y);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
